/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 20.02.12
 * Time: 17:21
 * To change this template use File | Settings | File Templates.
 */
function keyDown(evt) {
    keys[evt.keyCode] = true;
    //keylock[evt.keyCode] = false;

}

function keyUp(evt) {
    keys[evt.keyCode] = false;
    keylock[evt.keyCode] = false;
}

function updateGameSelecionKeys() {
    if (GameModeSelectionKEYS.F1 in keys && keys[GameModeSelectionKEYS.F1] && !gameModeSelected){
        gameModeSelected = true;
        initTwoPlayerGame();
    }
    if (GameModeSelectionKEYS.F2 in keys && keys[GameModeSelectionKEYS.F2] && !gameModeSelected){
        gameModeSelected = true;
        initOnePlayerGame();
    }
    if (GameModeSelectionKEYS.F3 in keys && keys[GameModeSelectionKEYS.F3] && !gameModeSelected){
        gameModeSelected = true;
        initTwoPlayerGame();
    }
}

function updateArrowKeys(player) {
    if (ControlKeysEnums.CTRL in keys && keys[ControlKeysEnums.CTRL]) { //ctrl
        alert("CTRL");
        canvasLostFocus();
    }

    if (player.assingedKeys == KeyBoardType.ARROWS){
        updateARROWS(player);
    }

    if (player.assingedKeys == KeyBoardType.WASD){
        updateWASD(player);

    }

}
function updateARROWS(player){

    if (ArrowKeysEnums.DOWN in keys && keys[ArrowKeysEnums.DOWN]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() + 1)) {
                player.moveY(1);
            }
        }
    }

    if (ArrowKeysEnums.LEFT in keys && keys[ArrowKeysEnums.LEFT]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() - 1, player.getPosY())) {
                player.moveX(-1);
            }
        }
    }

    if (ArrowKeysEnums.RIGHT in keys && keys[ArrowKeysEnums.RIGHT]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() + 1, player.getPosY())) {
                player.moveX(1);
            }
        }
    }

    if (ArrowKeysEnums.UP in keys && keys[ArrowKeysEnums.UP]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() - 1)) {
                player.moveY(-1);
            }
        }
    }

    if (ArrowKeysEnums.PLANT in keys && keys[ArrowKeysEnums.PLANT] && !keylock[ArrowKeysEnums.PLANT]) {
        keylock[ArrowKeysEnums.PLANT] = true;
        if (player.getActiveBombsCount() < player.maxBombsNumber) {
            player.plantBomb();
        }
    }
}

function updateWASD(player) {
    if (WASDEnums.DOWN in keys && keys[WASDEnums.DOWN]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() + 1)) {
                player.moveY(1);
            }
        }
    }

    if (WASDEnums.LEFT in keys && keys[WASDEnums.LEFT]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() - 1, player.getPosY())) {
                player.moveX(-1);
            }
        }
    }

    if (WASDEnums.RIGHT in keys && keys[WASDEnums.RIGHT]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() + 1, player.getPosY())) {
                player.moveX(1);
            }
        }
    }

    if (WASDEnums.UP in keys && keys[WASDEnums.UP]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() - 1)) {
                player.moveY(-1);
            }
        }
    }
    
    if (WASDEnums.PLANT in keys && keys[WASDEnums.PLANT] && !keylock[WASDEnums.PLANT]){
        keylock[WASDEnums.PLANT] = true;
        if (player.getActiveBombsCount() != player.maxBombsNumber) {
        	player.plantBomb();
        }
    }
}
